var form = document.querySelector('form');
var p = form.querySelectorAll('.dlmm p');
var arr = [true, true];
//1.判断手机号格式
var reg = /^1\d{10}$/;
form.number.onblur = function () {
    if (reg.exec(this.value)) {
        p[0].innerHTML = "&nbsp;"
        arr[0] = true;
    } else {
        p[0].innerHTML = "请输入正确的格式";
        arr[0] = false;
    }
}

//2.判断密码格式
var reg1 = /^\w{6}$/;
form.passd.onblur = function () {
    if (reg1.test(this.value)) {
        p[1].innerHTML = "&nbsp;"
        arr[1] = true;
    } else {
        p[1].innerHTML = "请输入正确的格式";
        arr[1] = false;
    }
}


var list = JSON.parse(localStorage.getItem('user'));
form.login.onclick = function () {
    form.number.focus();
    form.number.blur();
    form.passd.focus();
    form.passd.blur();
    if (arr[0] && arr[1]) {
        var res = list.some(function(item){
            return item.name == form.number.value;
        })
        if (res) {
            for (var i = 0; i < list.length; i++) {
                console.log(form.number.value);
                if (form.number.value == list[i].name && form.passd.value == list[i].pass) {
                    alert('登陆成功');
                    localStorage.setItem('phone', JSON.stringify(form.number.value));
                    window.open('../index.html')
                    return;
                }
            }
            alert("登陆失败");
        } else {
            alert("没有注册的用户，去注册");
            console.log(11);
            location.href = "./register.html"
            return false;
        }
    }
    return false;
}
